% RANDOMLY SELECT THE VARIETIES THAT WILL RECEIVE AN INNOVATION DRAW USING
% THE CONDITIONAL INNOVATION PROCEDURE:
        probability_draws_incumbents.US = rand(n_varieties,1);
        potentialincumbentsteals.US = probability_draws_incumbents.US < lambda.US;
        probability_draws_entrants.US = rand(n_varieties,1);
        potentialentrants.US = probability_draws_entrants.US < eta.US & ~potentialincumbentsteals.US;
        probability_draws_incumbents.OECD = rand(n_varieties,1);
        potentialincumbentsteals.OECD = probability_draws_incumbents.OECD < lambda.OECD & ~potentialincumbentsteals.US & ~potentialentrants.US;
        probability_draws_entrants.OECD = rand(n_varieties,1);
        potentialentrants.OECD = probability_draws_entrants.OECD < eta.OECD & ~potentialincumbentsteals.US & ~potentialentrants.US & ~potentialincumbentsteals.OECD;
          
for cc=1:length(place)

        
% CONDUCT A PARETO DRAW FOR EACH POTENTIALLY STOLEN VARIETY
        maybe_stolen.(place{cc}) = potentialentrants.(place{cc}) | potentialincumbentsteals.(place{cc});
        num_draws.(place{cc}) = sum(maybe_stolen.(place{cc}));
        
        if strcmp(innovation_mode,'consumed')
            productivitydraws.(place{cc}) = Pareto_draw(beta.(place{cc}),theta.(place{cc}), ... 
                qualities_vec_consumed.(place{cc})(maybe_stolen.(place{cc})),num_draws.(place{cc}));
        elseif strcmp(innovation_mode,'produced')        
            productivitydraws.(place{cc}) = Pareto_draw(beta.(place{cc}),theta.(place{cc}), ... 
                qualities_vec.(place{cc})(maybe_stolen.(place{cc})),num_draws.(place{cc}));
        elseif strcmp(innovation_mode,'partialspillover') 
            probability_draws_spillovers.(place{cc}) = rand(n_varieties,1);
            probability_draws_foreign.(place{cc}) = logical(probability_draws_spillovers.(place{cc})<kappa);
            qualities_vec_draw_from.(place{cc}) = qualities_vec.(place{cc});
            qualities_vec_draw_from.(place{cc})(probability_draws_foreign.(place{cc})) = ...
                qualities_vec_consumed.(place{cc})(probability_draws_foreign.(place{cc}));
            productivitydraws.(place{cc}) = Pareto_draw(beta.(place{cc}),theta.(place{cc}), ... 
                qualities_vec_draw_from.(place{cc})(maybe_stolen.(place{cc})),num_draws.(place{cc}));  
        else
              error('Innovation_mode in set_parameters.m needs to be set')
        end
        
        tmp_qual.(place{cc}) = -1*ones(size(qualities_vec.(place{cc})));
        tmp_qual.(place{cc})(maybe_stolen.(place{cc})) = productivitydraws.(place{cc});
        was_stolen.(place{cc}) = tmp_qual.(place{cc})>= qualities_vec.(place{cc});      
        incumbentsteals.(place{cc}) = potentialincumbentsteals.(place{cc}) & was_stolen.(place{cc});
        entrants.(place{cc}) = potentialentrants.(place{cc}) & was_stolen.(place{cc});
           
 % ALLOCATE STOLEN VARIETIES TO EXISTING INCUMBENTS WITHOUT REPLACEMENT
 % (first command is the real one - the other two are just for bizarre cases where incumbent steals are more than existing varieties or no varieties are owned by one country)
 if sum(incumbentsteals.(place{cc})) < length(varown_vec.(place{cc}).(year_index)(~isnan(varown_vec.(place{cc}).(year_index)))) && ~isempty(varown_vec.(place{cc}).(year_index)(~isnan(varown_vec.(place{cc}).(year_index))))
        who_stole.(place{cc}) = datasample(varown_vec.(place{cc}).(year_index)(~isnan(varown_vec.(place{cc}).(year_index)))...
            ,sum(incumbentsteals.(place{cc})),'Replace',false);
 elseif sum(incumbentsteals.(place{cc})) >= length(varown_vec.(place{cc}).(year_index)(~isnan(varown_vec.(place{cc}).(year_index)))) && ~isempty(varown_vec.(place{cc}).(year_index)(~isnan(varown_vec.(place{cc}).(year_index))))
        who_stole.(place{cc}) = datasample(varown_vec.(place{cc}).(year_index)(~isnan(varown_vec.(place{cc}).(year_index)))...
            ,sum(incumbentsteals.(place{cc})),'Replace',true);
 elseif isempty(varown_vec.(place{cc}).(year_index)(~isnan(varown_vec.(place{cc}).(year_index))))
        who_stole.(place{cc}) = datasample(varown_vec.(place{cc}).(year_index)...
            ,sum(incumbentsteals.(place{cc})),'Replace',false);
 end
 
 % UPDATE VARIETY OWNERSHIP TO STEALING INCUMBENTS  
 % (ENTRANT STEALS ARE DONE AFTER INTERNATIONAL EQUILIBRIUM DETERMINES
 % WHETHER A VARIETY IS PRODUCED BY US OR CANADA)
        varown_vec.(place{cc}).(year_index)(incumbentsteals.(place{cc})) = who_stole.(place{cc});
        
 % UPDATE VARIETY QUALITIES OF LEADING PRODUCER AND LEADING DOMESTIC
 % COMPETITOR
        domestic_competitor_qual_vec.(place{cc})(was_stolen.(place{cc})) = qualities_vec.(place{cc})(was_stolen.(place{cc}));
        qualities_vec.(place{cc})(was_stolen.(place{cc})) = tmp_qual.(place{cc})(was_stolen.(place{cc}));
        
end
    
    

 
